#include "spectral_div.h"
#if 0
/*This is the main entry point for the spectral div program*/
int main(int argc, char **argv)
{
	elem **GnrModMat, **ModMat, *S;
	int *Kg, *Group;
	int iMatrixSize,iGroupSize = 0, i, M;
	sparse_matrix_lst *A, *Ag;
	elem *u1, b1, *Fg, precision, deltaQ;

	if (argc != 4) 
	{
		return 0;
	}

	/*Get the precision from the input*/
	precision = atof(argv[3]);

	/*generate A_sparse*/	
	if (createSparseMatrixFromFile(argv[1],&A,&iMatrixSize) == 0)
	{
		return 0;
	}

	/*generate Group array*/
	if (createVertexGroupFromFile(argv[2], &iGroupSize, &Group, A->n) == 0)
	{
		free_sparse_matrix_lst(A);
		return 0;
	}

	if (CreateModularityMat(A, Group, iGroupSize, &ModMat, &Kg, &M) == 0)
	{
		free_sparse_matrix_lst(A);
		freeSafely(Group);
		return 0;
	}


	if (CreateGeneralizedModMat(ModMat, Group, iGroupSize, &GnrModMat, &Fg) == 0)
	{
		free_sparse_matrix_lst(A);
		freeSafely(Group);
		Free2dimensionalArray(ModMat, iGroupSize);
		freeSafely(Kg);
		return 0;
	}

	if (GetAgFromA(A, &Ag, Group, iGroupSize) == 0)
	{
		free_sparse_matrix_lst(A);
		freeSafely(Group);
		Free2dimensionalArray(ModMat, iGroupSize);
		freeSafely(Kg);
		Free2dimensionalArray(GnrModMat, iGroupSize);
		freeSafely(Fg);
		return 0;
	}

	if (calcLeadingEigenPair(Ag, GnrModMat, Kg, Fg, M, precision, &u1, &b1) == 0)
	{
		free_sparse_matrix_lst(A);
		freeSafely(Group);
		Free2dimensionalArray(ModMat, iGroupSize);
		freeSafely(Kg);
		Free2dimensionalArray(GnrModMat, iGroupSize);
		freeSafely(Fg);
		free_sparse_matrix_lst(Ag);
		return 0;
	}

	if (allocateVector(&S, iGroupSize) == 0)
	{
		free_sparse_matrix_lst(A);
		freeSafely(Group);
		Free2dimensionalArray(ModMat, iGroupSize);
		freeSafely(Kg);
		Free2dimensionalArray(GnrModMat, iGroupSize);
		freeSafely(Fg);
		free_sparse_matrix_lst(Ag);
		freeSafely(u1);
		return 0;
	}

	if (spectralDivideGroup(iGroupSize, u1, b1, Ag, GnrModMat, Kg, Fg, M, &deltaQ, S) == 0)
	{
		free_sparse_matrix_lst(A);
		freeSafely(Group);
		Free2dimensionalArray(ModMat, iGroupSize);
		freeSafely(Kg);
		Free2dimensionalArray(GnrModMat, iGroupSize);
		freeSafely(Fg);
		free_sparse_matrix_lst(Ag);
		freeSafely(u1);
		freeSafely(S);
		return 0;
	}

	/*Print the results:*/
	printf("%f\n", deltaQ);
	/*Print the first group contains the first index in the original group*/
	for (i=0; i<iGroupSize; i++)
	{
		if (S[i] == S[0])
			printf("%d ", Group[i]);
	}
	printf("\n");
	/*Print the second group*/
	for (i=0; i<iGroupSize; i++)
	{
		if (S[i] != S[0])
			printf("%d ", Group[i]);
	}
	
	free_sparse_matrix_lst(A);
	freeSafely(Group);
	Free2dimensionalArray(ModMat, iGroupSize);
	freeSafely(Kg);
	Free2dimensionalArray(GnrModMat, iGroupSize);
	freeSafely(Fg);
	free_sparse_matrix_lst(Ag);
	freeSafely(u1);
	freeSafely(S);
	return 1;
}
#endif